import { Exclude, Expose, Type, Transform } from 'class-transformer';
import * as moment from 'moment';
import { Moment } from 'moment';

export class BaseTransformer {
  @Exclude({ toPlainOnly: true })
  @Type(() => Date)
  @Transform(value => value ? moment(value) : null, { toClassOnly: true })
  createdTime: Moment

  // 创建时间转为驼峰并格式化时间格式
  @Expose({ name: 'created_time' })
  getCreatedTime() {
    return this.createdTime ? this.createdTime.format('YYYY-MM-DD HH:mm:ss') : undefined;
  }

  @Exclude({ toPlainOnly: true })
  @Type(() => Date)
  @Transform(value => value ? moment(value) : null, { toClassOnly: true })
  updatedTime: Moment

  // 更新时间转为驼峰并格式化时间格式
  @Expose({ name: 'updated_time' })
  getUpdatedTime() {
    return this.createdTime ? this.createdTime.format('YYYY-MM-DD HH:mm:ss') : undefined;
  }

  @Exclude({ toPlainOnly: true })
  createdBy:string

  @Exclude({ toPlainOnly: true })
  updatedBy:string

  // @Expose({ name: 'created_by' })
  // getCreatedBy() {
  //   return this.createdBy ?? undefined
  // }

  // 操作人
  @Transform(user => user ? { id: user.id, name: user.name} : null, { toClassOnly: true })
  operator: string;
  
  constructor(partial: Partial<BaseTransformer>) {
    Object.assign(this, partial);
  }
}

export class PageInfoBaseTransformer {
  data: []

  @Exclude({ toPlainOnly: true })
  pageSize: number | null

  @Exclude({ toPlainOnly: true })
  pageNo: number | null

  @Exclude({ toPlainOnly: true })
  totalPage: number | null

  @Exclude({ toPlainOnly: true })
  totalAccount: number | null

  @Expose({ name: 'page_size' })
  getPageSize() {
    return this.pageSize;
  }

  @Expose({ name: 'page_no' })
  getPageNo() {
    return this.pageNo;
  }

  @Expose({ name: 'total_page' })
  getTotalPage() {
    return this.totalPage;
  }

  @Expose({ name: 'total_account' })
  getTotalAccount() {
    return this.totalAccount;
  }
}